<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="代码," prop="code">
      <el-input v-model="dataForm.code" placeholder="代码,"></el-input>
    </el-form-item>
    <el-form-item label="别名" prop="alias">
      <el-input v-model="dataForm.alias" placeholder="别名"></el-input>
    </el-form-item>
    <el-form-item label="年级名字" prop="name">
      <el-input v-model="dataForm.name" placeholder="年级名字"></el-input>
    </el-form-item>
    <el-form-item label="学段别名" prop="sectionAlias">
      <el-input v-model="dataForm.sectionAlias" placeholder="学段别名"></el-input>
    </el-form-item>
    <el-form-item label="学年别名," prop="yearAlias">
      <el-input v-model="dataForm.yearAlias" placeholder="学年别名,"></el-input>
    </el-form-item>
    <el-form-item label="基因id," prop="gene">
      <el-input v-model="dataForm.gene" placeholder="基因id,"></el-input>
    </el-form-item>
    <el-form-item label="年级人数" prop="number">
      <el-input v-model="dataForm.number" placeholder="年级人数"></el-input>
    </el-form-item>
    <el-form-item label="年级说明" prop="descr">
      <el-input v-model="dataForm.descr" placeholder="年级说明"></el-input>
    </el-form-item>
    <el-form-item label="校本年级别名," prop="schoolAlias">
      <el-input v-model="dataForm.schoolAlias" placeholder="校本年级别名,"></el-input>
    </el-form-item>
    <el-form-item label="学校id" prop="schoolId">
      <el-input v-model="dataForm.schoolId" placeholder="学校id"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="创建者" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建者"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="更新者" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="更新者"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          code: '',
          alias: '',
          name: '',
          sectionAlias: '',
          yearAlias: '',
          gene: '',
          number: '',
          descr: '',
          schoolAlias: '',
          schoolId: '',
          createTime: '',
          createBy: '',
          updateTime: '',
          updateBy: ''
        },
        dataRule: {
          code: [
            { required: true, message: '代码,不能为空', trigger: 'blur' }
          ],
          alias: [
            { required: true, message: '别名不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '年级名字不能为空', trigger: 'blur' }
          ],
          sectionAlias: [
            { required: true, message: '学段别名不能为空', trigger: 'blur' }
          ],
          yearAlias: [
            { required: true, message: '学年别名,不能为空', trigger: 'blur' }
          ],
          gene: [
            { required: true, message: '基因id,不能为空', trigger: 'blur' }
          ],
          number: [
            { required: true, message: '年级人数不能为空', trigger: 'blur' }
          ],
          descr: [
            { required: true, message: '年级说明不能为空', trigger: 'blur' }
          ],
          schoolAlias: [
            { required: true, message: '校本年级别名,不能为空', trigger: 'blur' }
          ],
          schoolId: [
            { required: true, message: '学校id不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建者不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '更新者不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/eas/easbasicgrade/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.code = data.easBasicGrade.code
                this.dataForm.alias = data.easBasicGrade.alias
                this.dataForm.name = data.easBasicGrade.name
                this.dataForm.sectionAlias = data.easBasicGrade.sectionAlias
                this.dataForm.yearAlias = data.easBasicGrade.yearAlias
                this.dataForm.gene = data.easBasicGrade.gene
                this.dataForm.number = data.easBasicGrade.number
                this.dataForm.descr = data.easBasicGrade.descr
                this.dataForm.schoolAlias = data.easBasicGrade.schoolAlias
                this.dataForm.schoolId = data.easBasicGrade.schoolId
                this.dataForm.createTime = data.easBasicGrade.createTime
                this.dataForm.createBy = data.easBasicGrade.createBy
                this.dataForm.updateTime = data.easBasicGrade.updateTime
                this.dataForm.updateBy = data.easBasicGrade.updateBy
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/eas/easbasicgrade/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'code': this.dataForm.code,
                'alias': this.dataForm.alias,
                'name': this.dataForm.name,
                'sectionAlias': this.dataForm.sectionAlias,
                'yearAlias': this.dataForm.yearAlias,
                'gene': this.dataForm.gene,
                'number': this.dataForm.number,
                'descr': this.dataForm.descr,
                'schoolAlias': this.dataForm.schoolAlias,
                'schoolId': this.dataForm.schoolId,
                'createTime': this.dataForm.createTime,
                'createBy': this.dataForm.createBy,
                'updateTime': this.dataForm.updateTime,
                'updateBy': this.dataForm.updateBy
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
